#include "../../Command.h"

#include "../../User.h"

#include "../../NResource/Static.h"
#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

void ACK_CS_DUNGEON_INFO(User *pUser)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(-1) << static_cast<int8_t>(0);
	pAck->MakePacket(CMD_SN_PARTY_CREATE_DONE);
	pUser->Send(pAck);
}

void Command::CS_DUNGEON_INFO(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * //던전 정보요청 C->S 방장이 요청
		CS_DUNGEON_INFO		0x0801
		{
			s16		s16TableID;				//테이블ID
			s8		s8Difficult;			//난이도 (0: 노멀, 1: 하드, 2: 헬)
		}

		//던전 정보 S->C
		SC_DUNGEON_INFO		0x0802
		{
			s16		s16TableID;			/// 맵 테이블 아이디
			s8		s8Theme;				//테마
			s8		s8DungeonSize;			//던전 크기
			s8		s8ShortestPath;			//최단경로

			s16		s16MobTID0;				//몬스터 TID 0
			s16		s16MobTID1;				//몬스터 TID 1
			s16		s16MobTID2;				//몬스터 TID 2
			s16		s16MobTID3;				//몬스터 TID 3
			s16		s16MobTID4;				//몬스터 TID 4
			s16		s16MobTID5;				//몬스터 TID 5

			s16		s16LegendTID;			//레전드 TID
			s16		s16LegendAppearRate;	//레전드 등장 확률

			s16		s16BossTID;				//보스 TID;
			s16		s16BossMapTID;			//보스맵 TID;

			s16		s16MobLv0;				//등장 몬스터 레벨 (일반)
			s16		s16MobLv1;				//등장 몬스터 레벨 (일반)
			s16		s16MobLv2;				//등장 몬스터 레벨 (일반)
			s16		s16MobLv3;				//등장 몬스터 레벨 (일반)
			s16		s16MobLv4;				//등장 몬스터 레벨 (일반)
			s16		s16MobLv5;				//등장 몬스터 레벨 (일반)

			s16		s16LegendLv;			//등장 몬스터 레벨 (레전드)
			s16		s16BossLv;				//등장 몬스터 레벨 (보스)

			s8		s8Difficult;			//난이도 (0: 노멀, 1: 하드, 2: 헬)
		}
	 */

	STRESSLOG("CS_DUNGEON_INFO");

	PREPARE_PACKET;


	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_DUNGEON_INFO NULL == pSpace");
		return;
	}

	if (false == pSpace->IsDungeon())
	{
		DEBUGLOG("CS_DUNGEON_INFO space is not single or party dungeion");
		return;
	}

//	if (pSpace->GetData()._spaceType != NLogic::Space::SpaceType::SINGLE_DUNGEION)
//	{
//		return;
//	}

	uint16_t	table_id	= 0;
	char		difficulty	= 0;

	*pPacket >> table_id >> difficulty;
	
	if (difficulty < 0 || difficulty > 2)
	{
		DEBUGLOG("CS_DUNGEON_INFO : INVALID DIFFICULTY!! difficulty=%d", difficulty);
		return;
	}

	NResource::DungeonData::Flyweight * pFly =
				NResource::Static::InstancePtr()->GetDungeon()->GetFly(pUser->GetUserInfo().company, pUser->GetUserInfo().sale_code, table_id);

	if (pFly == NULL)
	{
		DEBUGLOG("CS_DUNGEON_INFO pFly == NULL company=%d, salecode=%d, tableid=%d", pUser->GetUserInfo().company, pUser->GetUserInfo().sale_code, table_id);
		return;
	}


	int level = pUser->GetCharacterInfo().level;

	NResource::DungeonEnterLimitLevel * pDungeonEnterLimitLevel = NResource::Static::Instance().GetDungeonEnterLimitLevel();
	if (NULL == pDungeonEnterLimitLevel)
	{
		ACK_CS_DUNGEON_INFO(pUser);
		return;
	}

	int limitLevel = pDungeonEnterLimitLevel->GetLevel(table_id);
	if (level < limitLevel)
	{
		ACK_CS_DUNGEON_INFO(pUser);
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	pSpace->GetData()._boss_tid		= pFly->BOSS_TID;
	pSpace->GetData()._dungeonTheme	= pFly->THEME;
	pSpace->GetData()._table_id 	= table_id;
	pSpace->GetData()._difficulty 	= difficulty;

	*pAck << table_id;
	*pAck << static_cast<char>(pFly->THEME);
	*pAck << static_cast<char>(pFly->SIZE);
	*pAck << static_cast<char>(pFly->LENGTH);


	*pAck << static_cast<uint16_t>(pFly->MOB1_TID);
	*pAck << static_cast<uint16_t>(pFly->MOB2_TID);
	*pAck << static_cast<uint16_t>(pFly->MOB3_TID);
	*pAck << static_cast<uint16_t>(pFly->MOB4_TID);
	*pAck << static_cast<uint16_t>(pFly->MOB5_TID);
	*pAck << static_cast<uint16_t>(pFly->MOB6_TID);



	*pAck << static_cast<uint16_t>(pFly->LEGEND_TID);
	*pAck << static_cast<uint16_t>(pFly->LEGEND_APPEAR_RATE);


	*pAck << static_cast<uint16_t>(pFly->BOSS_TID);
	*pAck << static_cast<uint16_t>(pFly->BOSS_MAP);


	if (pSpace->GetData()._difficulty == 0)
	{

		*pAck << static_cast<uint16_t>(pFly->MOB1_LV_NORMAL_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->MOB2_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->MOB3_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->MOB4_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->MOB5_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->MOB6_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->LEGEND_LV_NORMAL);
		*pAck << static_cast<uint16_t>(pFly->BOSS_LV_NORMAL);

	}
	else if (pSpace->GetData()._difficulty == 1)
	{

		*pAck << static_cast<uint16_t>(pFly->MOB1_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->MOB2_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->MOB3_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->MOB4_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->MOB5_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->MOB6_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->LEGEND_LV_HARD);
		*pAck << static_cast<uint16_t>(pFly->BOSS_LV_HARD);

	}
	else
	{

		*pAck << static_cast<uint16_t>(pFly->MOB1_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->MOB2_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->MOB3_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->MOB4_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->MOB5_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->MOB6_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->LEGEND_LV_HELL);
		*pAck << static_cast<uint16_t>(pFly->BOSS_LV_HELL);
	}

	*pAck << difficulty;

	pAck->MakePacket(CMD_SC_DUNGEON_INFO);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/



